home *** CD-ROM | disk | FTP | other *** search
- Path: newsstand.tc.umn.edu!ianhogg
- From: ianhogg@cs.umn.edu (Ian J Hogg)
- Newsgroups: comp.lang.c++
- Subject: Problem with enums and ostrstreams
- Date: 21 Feb 1996 16:43:57 GMT
- Organization: University of Minnesota
- Message-ID: <4gfi4d$kpj@epx.cis.umn.edu>
- NNTP-Posting-Host: tera.cs.umn.edu
-
- I've been having a problem that the following code is a good example
- of:
-
- #include <iostream.h>
- #include <strstream.h>
-
- enum Codes { GOOD, BAD };
-
- ostrstream & operator << (ostrstream &ostr, ostrstream & (*func)(ostrstream &))
- {
- cout << "in ostrstream manip func" << endl;
- return func(ostr);
- }
-
- int main(int, char **)
- {
- ostrstream msg;
- msg << GOOD << ends;
- cout << "msg = '" << msg.str() << "'" << endl;
- return 0;
- }
-
- 1. HPUX with HP C++ HPCPLUSPLUS A.03.72 everthing is fine.
- 2. On solaris with C++4.0.1 it compiles cleanly but when GOOD is inserted
- into the ostrstream, it calls the manip function.
- 3. OSF/1 (DEC Alpha), the compiler say "msg<<GOOD" is ambiguous.
-
-
- The two problems only occur when the enum's value is zero. It appears to me
- that these compilers are treating the enums as literals. Is this behavior
- correct? I've been able to work around the sun problem as follows:
-
- ostrstream & operator << (ostrstream &ostr, ostrstream & (*func)(ostrstream &))
- {
- if (func == 0) {
- int zero = 0;
- ostr << zero;
- } else
- func(ostr);
- return ostr;
- }
- --
- ===============================================================================
- Ian Hogg ianhogg@cs.umn.edu
- (612) 424-6332
-